package com.henanuniversity.dao.impl;

import com.henanuniversity.dao.BusinessDao;
import com.henanuniversity.po.Business;
import com.henanuniversity.utils.DBUtils;
import com.henanuniversity.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) {
        ArrayList<Business> list = new ArrayList<>();
        String sql="select * from business where orderTypeId=? order by businessId";
        try {
            conn= DBUtils.getConnection();
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1,orderTypeId);
            ResultSet rs = pst.executeQuery();
            while(rs.next()){
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                list.add(business);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);

        }
        return list;
    }

    @Override
    public Business getBusinessById(Integer businessId) {
        Connection conn=null;
        PreparedStatement pst=null;
        ResultSet rs=null;
        Business business=null;
        String sql="select * from business where businessId=?";
        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setInt(1,businessId);
            rs=pst.executeQuery();
            if(rs.next()){
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(rs,pst);
        }


        return business;
    }
}
